package com.itheima.controller;

import com.itheima.pojo.entity.Emp;
import com.itheima.pojo.entity.Result;
import com.itheima.service.EmpService;
import com.itheima.service.impl.EmpServiceA;
import com.itheima.utils.XmlParserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wuJiaWei
 * @version 1.0
 */
@RestController //@RestController = @Controller + @ResponseBody
public class EmpController {
    @Autowired //运行时，IOC容器会提供该类型的bean对象，并赋值给该变量
    private EmpService empService;
    @RequestMapping("/listEmp")
    public Result list(){
        //1.加载并解析emp.xml
        String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
        List<Emp> empList = XmlParserUtils.parse(file, Emp.class);

        //2.数据转换处理
        empList.forEach(emp -> {
           //处理gender 1:男，2:女
            String gender = emp.getGender();
            if("1".equals(gender)){
                emp.setGender("男");
            }else if("2".equals(gender)){
                emp.setGender("女");
            }
            //处理job 1:讲师，2:班主任，3:就业指导
            String job = emp.getJob();
            if("1".equals(job)){
                emp.setJob("讲师");
            }else if("2".equals(job)){
                emp.setJob("班主任");
            }else if("3".equals(job)){
                emp.setJob("就业指导");
            }
        });

        //3.响应数据
        return Result.success(empList);
    }

    @RequestMapping("/listEmp2")
    public Result list2(){
        List<Emp> empList = empService.listEmp();
        //3.响应数据
        return Result.success(empList);
    }
}
